package cn.itcast.bos.web.action.base;

import java.util.Date;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.domain.system.User;
import cn.itcast.bos.service.base.IStandardService;
import cn.itcast.bos.web.action.common.BaseAction;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Actions
@Scope("prototype")
public class StandardAction extends BaseAction<Standard> {
    // 模型驱动
    // private Standard standard=new Standard();
    // @Override
    // public Standard getModel() {
    // // TODO Auto-generated method stub
    // return standard;
    // }
    // 注入service
    @Autowired
    private IStandardService standardServiceImpl;

    // 添加收派标准
    @Action(value = "standard_save", results = {
            @Result(name = "success", type = "redirect", location = "./pages/base/standard.html") })
    public String saveStandard() {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        // 将当前时间转换为2017-7-26 21:35:13格式
        // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // java.util.Date date = null;
        // try {
        // date = sdf.parse(sdf.format(new Date()));
        // System.out.println(date);
        // } catch (ParseException e) {
        // // TODO Auto-generated catch block
        // e.printStackTrace();
        // }
        model.setOperatingTime(new Date());

        model.setOperator(user.getUsername());
        System.out.println(user.getStation() + "-----------------============");
        model.setOperatingCompany(user.getStation());
        standardServiceImpl.saveStandard(model);

        return SUCCESS;
    }

    // 分页列表查询
    // 属性驱动
    // private int page;
    // private int rows;
    //
    // public void setPage(int page) {
    // this.page = page;
    // }
    //
    // public void setRows(int rows) {
    // this.rows = rows;
    // }

    @Action(value = "standard_pageQuery", results = { @Result(name = "success", type = "json") })
    public String pageQueryStandard() {
        // 将页码，查询条数封装PageRequest，PagingAndSortingRepository中的Page<T> findAll(Pageable pageable);
        Pageable pageable = new PageRequest(page - 1, rows);
        Page<Standard> pages = standardServiceImpl.pageQueryStandard(pageable);
        // 返回客户端 总记录数total 当前页面数据 rows
        // Map<String, Object> map = new HashMap<String, Object>();
        // map.put("total", pages.getTotalElements());
        // System.out.println(pages.getTotalElements());
        // map.put("rows", pages.getContent());
        // // 将map转换为json数据返回，这里将数据放入valueStack中
        // ActionContext.getContext().getValueStack().push(map);
        pushPageDataToValueStack(pages);
        return SUCCESS;
    }

    // 查询所有收派信息
    @Action(value = "standard_findAll", results = { @Result(name = "success", type = "json") })
    public String findAllStandard() {
        List<Standard> standards = standardServiceImpl.findAllStandard();
        ActionContext.getContext().getValueStack().push(standards);
        return SUCCESS;
    }

}
